<?php
$objXajax->registerFunction('frmunitcity_LoadData');
$objXajax->registerFunction('frmunitcity_DeleteData');
$objXajax->registerFunction('frmunitcity_save');
$objXajax->registerFunction('frmunitcity_LoadCbxCity');
$objXajax->registerFunction('frmunitcity_LoadCbxDistrict');
$objXajax->registerFunction('frmunitcity_edit'); 
$objXajax->registerFunction('frmunitcity_activeunit'); 
$lang ='vi';
function frmunitcity_LoadData($cmd,$id_city='',$id_district=''){ 
    global $lang,$objDbSelect,$path;
    $sql = "";
    $swhere ="";  
    if ($cmd == 1)
    {
        $sql = "SELECT id,name as tinh,longcenter as lng,latcenter as lat,active as active FROM tbl_city order by name";
    }
    else if ($cmd == 2)
    {
       $swhere = '';
        if($id_city!=''){
            $swhere = " WHERE d.city_id = '$id_city' ";    
        }
       
       $sql = "SELECT d.id,c.name as tinh,d.name as quan,d.longcenter as lng,d.latcenter as lat,d.active as active FROM tbl_district d INNER JOIN tbl_city c ON d.city_id = c.id $swhere order by c.name,d.name"; 
    }
    else if ($cmd == 3)
    {
        $swhere = "WHERE w.city_id = '$id_city' ";
        if($id_district!=''){
            $swhere .= "  AND w.district_id = $id_district "; 
        }        
        
      $sql = "SELECT w.id,c.name as tinh,d.name as quan,concat(c.name,' - ',d.name) as tinhquan,w.name as xa,w.longcenter as lng,w.latcenter as lat,w.active as active FROM tbl_ward w  
      INNER JOIN tbl_city c ON w.city_id = c.id  INNER JOIN tbl_district d ON w.district_id = d.id $swhere order by c.name,d.name,w.name"; 
    }
    $objResponse = new xajaxResponse();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/main_unitcity_grid_'.$cmd.'.html');
    $oTbs->MergeBlock('blk_data',$objDbSelect->_connectionID,$sql);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmunitcity_divGrid', 'innerHTML', $oTbs->Source); 
    if ($cmd == 1)
    {
     $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmunitcity_tbl');");                                    
    }
    else if ($cmd == 2)    
    {
      $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('frmunitcity_tbl');");                        
    }
    else if ($cmd == 3)    
    {
      $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('frmunitcity_tbl');");                        
    }
    $objResponse->addScript('changeWaiting(1);'); 
    return $objResponse->getXML();
}
function frmunitcity_DeleteData($arrID,$cmd){ 
    global $lang,$objDbUpdate;
    $objResponse = new xajaxResponse();
    $sql='';
    if ($cmd == 1)
    {
     $sql = 'DELETE FROM tbl_city WHERE id IN ('.implode(', ', $arrID).')';
    }
    else if ($cmd == 2)
    {
     $sql = 'DELETE FROM tbl_district WHERE id IN ('.implode(', ', $arrID).')';    
    }
    else if ($cmd == 3)
    {
     $sql = 'DELETE FROM tbl_ward WHERE id IN ('.implode(', ', $arrID).')';       
    }
    $rs = $objDbUpdate->Execute($sql);
    $objResponse->addScript('$("#btnSave").html("Lưu");');   
     
    return $objResponse->getXML();
}
function frmunitcity_LoadCbxCity(){
    global $lang,$objDbSelect;
    $objResponse = new xajaxResponse();
    $sql = 'SELECT id,name as city_name FROM tbl_city order by name';
    $oTbs = new clsTinyButStrong();      
    $oTbs->Source = '<select id="frmunitcity_cbxCity" name="frmunitcity_cbxCity" onchange="fn_frmunitcity_changeCity(this.value)" style="width:250px">';
    $oTbs->Source .='<option value="">-- Chọn --</option>';
    $oTbs->Source .='<option value="[blk_city.id;block=option;]"> [blk_city.city_name]</option>';
    $oTbs->Source .='</select';
    $oTbs->MergeBlock('blk_city',$objDbSelect->_connectionID,$sql);
  //  echo $sql; die();
    $objResponse->addAssign('frmcity_divcbxCity', 'innerHTML', $oTbs->Source);  
    return $objResponse->getXML();
}
function frmunitcity_LoadCbxDistrict($id_city,$district_id='-1'){ 
    global $lang,$objDbSelect;
    $objResponse = new xajaxResponse();
    $sql = "SELECT id,name as district_name FROM tbl_district WHERE city_id = '$id_city' order by name";
    $oTbs = new clsTinyButStrong();      
    $oTbs->Source = '<select id="frmunitcity_cbxDistrict" name="frmunitcity_cbxDistrict" onchange="fn_frmunitcity_changeWard(this.value)" style="width:250px">';
    $oTbs->Source .='<option value="">-- Chọn --</option>';
    $oTbs->Source .='<option value="[blk_ward.id;block=option;]"> [blk_ward.district_name]</option>';
    $oTbs->Source .='</select';
    $oTbs->MergeBlock('blk_ward',$objDbSelect->_connectionID,$sql);
    $objResponse->addAssign('frmcity_divcbxDistrict', 'innerHTML', $oTbs->Source);
    $objResponse->addScript('$("#frmunitcity_cbxDistrict").val("'.$district_id.'");');  
    return $objResponse->getXML();
}
function frmunitcity_save($arrParam){
    global $lang,$objDbSelect,$arrayData;
    $objResponse = new xajaxResponse();
    $sql ='';
    $edit =  $arrParam['hdEdit'];
    $id =  $arrParam['hdId'];
    $lat= $arrParam['hdlat'];
    $lng= $arrParam['hdlng'];
    $user_id = $_SESSION['userid'];
    $cmd =1;
    $citygroup='';
    $districtgroup='';
    if(isset($arrParam['citygroup'])&&$arrParam['citygroup']==1){
        $name = $arrParam['txtcity'];
       // $citygroup= $arrParam['txtcity'];
        if($edit!=1){
            $sql = "INSERT INTO tbl_city (name,user_created,date_created,user_update,date_update,longcenter,latcenter,active) 
                VALUES('$name','$user_id',now(),'$user_id',now(),'$lng','$lat',1)";        
        }else{
           $sql = "UPDATE  tbl_city SET name='$name',user_update='$user_id',date_update=now(),longcenter='$lng',latcenter='$lat' WHERE id = '$id'"; 
        }
        
    }elseif(isset($arrParam['districtgroup'])&&$arrParam['districtgroup']==2){
        
        $name = $arrParam['txtdistrict'];
        $city_id = $arrParam['cbxCity'];
        $citygroup = $city_id;
        $cmd=2; 
        if($edit!=1){
            $sql = "INSERT INTO tbl_district (name,city_id,user_created,date_created,user_update,date_update,longcenter,latcenter,active) 
                VALUES('$name','$city_id','$user_id',now(),'$user_id',now(),'$lng','$lat',1)";     
        }else{
           $sql = "UPDATE tbl_district  SET city_id='$city_id', name='$name',user_update='$user_id',date_update=now(),longcenter='$lng',latcenter='$lat' WHERE id = $id";
        }
        
    }else{
        
        $name = $arrParam['txtward'];
        $city_id = $arrParam['cbxCity'];  
        $district_id = $arrParam['cbxDistrict'];
        $citygroup = $city_id;  
        $districtgroup = $district_id;
        $cmd=3;         
        if($edit!=1){

           $sql = "INSERT INTO tbl_ward (name,city_id,district_id,user_created,date_created,user_update,date_update,longcenter,latcenter,active) 
            VALUES('$name','$city_id','$district_id','$user_id',now(),'$user_id',now(),'$lng','$lat',1)";     
        }else{
           $sql = "UPDATE  tbl_ward SET city_id='$city_id',district_id='$district_id',name='$name',
           user_update='$user_id',date_update=now(),longcenter='$lng',latcenter='$lat' WHERE id = $id";
        }
        
    }

    $objDbSelect->Execute($sql);
     $objResponse->addScript("xajax_frmunitcity_LoadData($cmd,'$citygroup','$districtgroup');");
    return $objResponse->getXML();
}

/*
* 
* 
*/
function frmunitcity_edit($id,$cmd=1)
{

    global $lang,$objDbSelect;
    $objResponse = new xajaxResponse();
    $sql='';
    // $sql = "SELECT name as city_name,latcenter,longcenter FROM tbl_city WHERE id = '$id'";
    
    if ($cmd == 1)
    {
      $sql = "SELECT name as city_name,latcenter,longcenter FROM tbl_city WHERE id = '$id'";
     
    }
    else if($cmd == 2)
    {
      $sql = "SELECT city_id,name as district_name,latcenter,longcenter FROM tbl_district WHERE id = '$id'";  
    }
    else if($cmd == 3)
    {
      $sql = "SELECT city_id,district_id,name ward_name,latcenter,longcenter FROM tbl_ward WHERE id = '$id'";  
    }
   
    $rs = $objDbSelect->Execute($sql);
    $city_name  = isset($rs->fields['city_name']) ? $rs->fields['city_name'] : '';
    $city_id  = isset($rs->fields['city_id']) ? $rs->fields['city_id'] : -1;
    $district_id  = isset($rs->fields['district_id']) ? $rs->fields['district_id'] : -1;
    $district_name  = isset($rs->fields['district_name']) ? $rs->fields['district_name'] : '';
    $ward_name  = isset($rs->fields['ward_name']) ? $rs->fields['ward_name'] : '';
    $latcenter  = isset($rs->fields['latcenter']) ? $rs->fields['latcenter'] : 0;
    $longcenter  = isset($rs->fields['longcenter']) ? $rs->fields['longcenter'] : 0;
    
    $objResponse->addScript('$("#frmunitcity_txtcity").val("'.$city_name.'");');
    $objResponse->addScript('$("#frmunitcity_txtdistrict").val("'.$district_name.'");');
    $objResponse->addScript('$("#frmunitcity_txtward").val("'.$ward_name.'");');
    $objResponse->addScript('$("#frmunitcity_cbxCity").val("'.$city_id.'");');
    $objResponse->addScript('xajax_frmunitcity_LoadCbxDistrict("'.$city_id.'","'.$district_id.'");');
    $objResponse->addScript('createMarker('.$latcenter.','.$longcenter.');');
    $objResponse->addScript('$("#hdidEdit").val("'.$id.'");');
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();  
}

/*
* 
* 
*/
function frmunitcity_edit_district($id)
{

    global $lang,$objDbSelect;
    $objResponse = new xajaxResponse();
    $sql='';
    $sql = "SELECT city_id,name as district_name,latcenter,longcenter FROM tbl_district WHERE id = '$id'";  
   
    $rs = $objDbSelect->Execute($sql);
    $city_name  = isset($rs->fields['city_name']) ? $rs->fields['city_name'] : '';
    $city_id  = isset($rs->fields['city_id']) ? $rs->fields['city_id'] : -1;
    $district_id  = isset($rs->fields['district_id']) ? $rs->fields['district_id'] : -1;
    $district_name  = isset($rs->fields['district_name']) ? $rs->fields['district_name'] : '';
    $ward_name  = isset($rs->fields['ward_name']) ? $rs->fields['ward_name'] : '';
    $latcenter  = isset($rs->fields['latcenter']) ? $rs->fields['latcenter'] : 0;
    $longcenter  = isset($rs->fields['longcenter']) ? $rs->fields['longcenter'] : 0;
    
    $objResponse->addScript('$("#frmunitcity_txtcity").val("'.$city_name.'");');
    $objResponse->addScript('$("#frmunitcity_txtdistrict").val("'.$district_name.'");');
    $objResponse->addScript('$("#frmunitcity_txtward").val("'.$ward_name.'");');
    $objResponse->addScript('$("#frmunitcity_cbxCity").val("'.$city_id.'");');
    $objResponse->addScript('xajax_frmunitcity_LoadCbxDistrict("'.$city_id.'","'.$district_id.'");');
    $objResponse->addScript('createMarker('.$latcenter.','.$longcenter.');');
    $objResponse->addScript('$("#hdidEdit").val("'.$id.'");');
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();  
}

/*
* 
* 
*/
function frmunitcity_edit_ward($id)
{

    global $lang,$objDbSelect;
    $objResponse = new xajaxResponse();
    $sql='';
    $sql = "SELECT city_id,district_id,name ward_name,latcenter,longcenter FROM tbl_ward WHERE id = '$id'";  
    
    $rs = $objDbSelect->Execute($sql);
    $city_name  = isset($rs->fields['city_name']) ? $rs->fields['city_name'] : '';
    $city_id  = isset($rs->fields['city_id']) ? $rs->fields['city_id'] : -1;
    $district_id  = isset($rs->fields['district_id']) ? $rs->fields['district_id'] : -1;
    $district_name  = isset($rs->fields['district_name']) ? $rs->fields['district_name'] : '';
    $ward_name  = isset($rs->fields['ward_name']) ? $rs->fields['ward_name'] : '';
    $latcenter  = isset($rs->fields['latcenter']) ? $rs->fields['latcenter'] : 0;
    $longcenter  = isset($rs->fields['longcenter']) ? $rs->fields['longcenter'] : 0;
    
    $objResponse->addScript('$("#frmunitcity_txtcity").val("'.$city_name.'");');
    $objResponse->addScript('$("#frmunitcity_txtdistrict").val("'.$district_name.'");');
    $objResponse->addScript('$("#frmunitcity_txtward").val("'.$ward_name.'");');
    $objResponse->addScript('$("#frmunitcity_cbxCity").val("'.$city_id.'");');
    $objResponse->addScript('xajax_frmunitcity_LoadCbxDistrict("'.$city_id.'","'.$district_id.'");');
    $objResponse->addScript('createMarker('.$latcenter.','.$longcenter.');');
    $objResponse->addScript('$("#hdidEdit").val("'.$id.'");');
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();  
}

function frmunitcity_activeunit($id,$type){
      global $objDbSelect;
    $objResponse = new xajaxResponse();
    $sql='';
    $table = 'tbl_ward';
    if($type==2){
        $table= 'tbl_district';
    }
    $sql = "UPDATE $table SET active = (active+1)%2 WHERE id = '$id'";  
    
    $rs = $objDbSelect->Execute($sql);
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();    
}
?>